<template>
  <div class="wxcode">
    <nav>
      <div class="nav-left"><img src="../../assets/nav-logo.png" alt="" @click="$router.push('/')"/></div>
    </nav>
    <div class="wxcode-left">
      <h3>扫码登录</h3>
      <a-spin tip="加载中..." :spinning="loginSpinning">
        <div class="wxcode-left-img">
          <div class="zzc" v-show="zzcShow">
            <!-- <a-button @click="getCode" style="margin-bottom: 20px">刷新</a-button> -->
            <a-button type="primary" @click="getCode" style="width: 100px"> 刷新 </a-button>
          </div>
          <div class="zzc2" v-if="smShow">
            <a-icon type="check-circle" />
            <p style="color: #000">扫码成功</p>
          </div>
          <img :src="imgurl" alt="" id="loginImg" @load="imgLoad" />
          <p>请使用微信扫码登录，如未完成组织认证还需进行认证</p>
          <p>30分钟后失效</p>
          <a-button type="primary" style="margin-top: 10px" @click="resetImg"> 刷新重试 </a-button>
        </div>
      </a-spin>
    </div>
    <div class="wxcode-right"></div>
  </div>
</template>


<script>
import { mapActions } from 'vuex'
import { timeFix, setToken } from '@/utils/util'
import { getRandom, getProtocol } from '@/utils/publicFun'
import { upRole } from '@/api/user'
export default {
  data() {
    return {
      scan_flag: 0,
      imgurl: null,
      loginSpinning: false,
      setIntervalNum: 0,
      setInter: null,
      zzcShow: false,
      smShow: false,
    }
  },
  mounted() {
    this.getCode()
  },
  watch: {
    setIntervalNum: function (newValue, oldValue) {
      console.log(newValue)
      if (this.setIntervalNum == 360) {
        clearInterval(this.setInter)
        this.setIntervalNum = 0
        this.smShow = false
        this.zzcShow = true
      }
    },
  },
  methods: {
    imgLoad() {
      this.loginSpinning = false
      this.zzcShow = false
      this.smShow = false
      this.intervalUpRole()
    },
    // 获取认证码
    getCode() {
      this.loginSpinning = true
      this.scan_flag = getRandom(10)
      let url = `https://api.gxzg.org.cn/tools/Qrcode?isSetLogo=0&download=0&code=100100&sign=ffddf97d212fdf28483aaff25cfd0a16&url=${getProtocol()}/license?scan_flag=`
      this.imgurl = url + this.scan_flag
    },
    // 轮询看是否扫码认证
    intervalUpRole() {
      let _that = this
      this.setInter = setInterval(function () {
        _that.setIntervalNum += 1
        // { scan_flag: '1' }
        upRole({ scan_flag: _that.scan_flag })
          .then((res) => {
            _that.smShow = true
            if (res.data.role == 'company_user' || res.data.role == 'op_user') {
              _that.loginSuccess(res.data)
            }
          })
          .catch((err) => {
            console.log(err)
            if (err.data.is_scaned == '1') {
              _that.smShow = true
            }
          })
      }, 5000)
    },
    loginSuccess(res) {
      const redirect = decodeURIComponent(this.$route.query.redirect || '/')
      setToken('pssid', res.authorization)
      // 产品说统一回到首页去，不需要重定向了
      this.$router.replace({ path: '/index' })

      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        this.$notification.success({
          message: '欢迎',
          description: '登录成功',
        })
      }, 1000)
      this.isLoginError = false
      this.smShow = false
    },
    // 刷新重试
    resetImg() {
      this.imgurl = null
      this.clear()
      this.getCode()
    },
    // 清除定时器
    clear() {
      clearInterval(this.setInter)
      this.setIntervalNum = 0
    },
  },
  destroyed() {
    // 离开就清除定时器
    this.clear()
  },
}
</script>


<style lang="less" scoped>
.wxcode {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 140px 0 144px;
  nav {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 64px;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 2px 2px 2px 2px #eee;
    padding: 0 18%;
    box-sizing: border-box;
    .nav-left {
      color: #1890ff;
      font-size: 20px;
      font-weight: bold;
      cursor: pointer;
    }
  }
  &-left {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    // width: 300px;
    // margin-left: 18%;
    h3 {
      font-weight: bold;
      line-height: 1;
    }
    &-img {
      padding: 20px 30px 10px;
      width: 400px;
      height: 360px;
      box-sizing: border-box;
      // margin-bottom: 30px;
      text-align: center;
      border: 1px solid rgb(226, 223, 223);
      border-radius: 4px;
      position: relative;
      background: #fff;
      img {
        max-width: 182px;
        max-height: 182px;
        margin-bottom: 50px;
      }
      p {
        color: rgb(104, 102, 102);
        margin-bottom: 0;
      }
      P:last-child {
        margin-top: 10px;
      }
      .zzc {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(255, 255, 255, 0.9);
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 100;
      }
      .zzc2 {
        position: absolute;
        width: 182px;
        height: 182px;
        left: 50%;
        transform: translate(-50%);
        background: rgba(255, 255, 255, 0.9);
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        i {
          font-size: 40px;
          color: #07c160;
        }
      }
    }
  }
  &-right {
    width: 300px;
    height: 300px;
    background: url('../../assets/daBG.png') no-repeat center;
    background-size: 300px 300px;
    margin: 0 0 0 150px;
  }
}
</style>